package com.wyl.niupi.frameworkExt.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wyl.niupi.framework.mybatisPlusExt.GroupBy;
import com.wyl.niupi.framework.mybatisPlusExt.Where;
import com.wyl.niupi.framework.util.controller.BaseController;
import com.wyl.niupi.framework.util.http._HttpRequestUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 增强BaseController，<b>可以放普遍的业务方法</b>
 *
 * @author 吴宇亮 on 2019/2/7 0007 10:40
 */
public class BaseControllerExt extends BaseController {

    protected IPage getPage(HttpServletRequest request) {
        String page = request.getParameter("page");
        String rows = request.getParameter("rows");
        return StringUtils.isNoneBlank(page, rows) ? new Page(Integer.valueOf(page), Integer.valueOf(rows)) : null;
    }

    protected <T> QueryWrapper<T> getQueryWrapper(HttpServletRequest request) {
        QueryWrapper<T> queryWrapper = new QueryWrapper();
        Map<String, String> requestMap = _HttpRequestUtil.getRequestMap(request);
        for (String key : requestMap.keySet()) {
            key = key.toLowerCase();
            if(key.startsWith(Where.NAME)){
                String value = requestMap.get(key);
                Where.autoFilter(key, value, queryWrapper);
            }else if(key.startsWith(GroupBy.NAME)){
                GroupBy.autoFilter(key, queryWrapper);
            }
        }
        return queryWrapper;
    }
}
